package com.liangtian.mdm.system.datapermission.service.impl;

import com.liangtian.mdm.system.datapermission.dao.SysDataPermissionDao;
import com.liangtian.mdm.system.datapermission.model.SysDataPermission;
import com.liangtian.mdm.system.datapermission.service.SysDataPermissionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SysDataPermissionServiceImpl implements SysDataPermissionService {

    @Autowired
    private SysDataPermissionDao sysDataPermissionDao;

    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    @Override
    public List<SysDataPermission> getDataPermissionLstByCond(
            SysDataPermission permission) {
        return sysDataPermissionDao.getDataPermissionLstByCond(permission);
    }

    @Override
    public void saveDataPermission(String[] permissionArray, String userId,
                                   String dpType) {
        String tmpArray[] = null;
        String lbbm = "";
        String oprtStr = "";

        //避免冗余数据
        List<String> dlLst = new ArrayList<String>();
        List<String> zlLst = new ArrayList<String>();

        String test = "";
        String[] newArray = this.toLbPermissionArray(permissionArray);
        for (String tmp : newArray) {
            tmpArray = tmp.split("\\|\\|");
            lbbm = tmpArray[0];
            oprtStr = tmpArray[1];
            if ("add".equalsIgnoreCase(oprtStr)) {
                if (lbbm.length() == 2) {
                    dlLst.add(lbbm);
                    insertDataPermission(lbbm, userId, dpType);
                } else if (lbbm.length() == 4) {
                    zlLst.add(lbbm);
                    test = lbbm.substring(0, 2);
                    if (!dlLst.contains(test))//防止用户先展开大类下的中类，然后勾选大类，这样有变化的是大类和大类下的所有中类，但数据库只需要大类
                        insertDataPermission(lbbm, userId, dpType);
                } else if (lbbm.length() == 6) {
                    test = lbbm.substring(0, 4);
                    if (!zlLst.contains(test))//防止用户先展开中类下的小类，然后勾选中类，这样有变化的是中类和中类下的所有小类，但数据库只需要中类
                        insertDataPermission(lbbm, userId, dpType);
                }
            } else {
                deleteDataPermission(lbbm, userId, dpType);
            }
        }

    }

    private void insertDataPermission(String lbbm, String userId, String permissionType) {
        SysDataPermission cond = new SysDataPermission();
        cond.setUser_id(userId);
        cond.setPermission_type(permissionType);
        List<SysDataPermission> dpLst = new ArrayList<SysDataPermission>();
        //如果新增一大类，那么存储到后台的形式为	01	null	null	即中类和小类为空的形式
        if (lbbm.length() == 2) {
            cond.setDl_id(lbbm);
        }
        //如果新增一中类，那么存储到后台的形式为	01	0101	null	即小类为空的形式
        else if (lbbm.length() == 4) {    //新增一中类
            String dlCode = lbbm.substring(0, 2);
            cond.setDl_id(dlCode);
            cond.setZl_id(lbbm);
        }
        //如果新增一小类，那么存储到后台的形式为	01	0101	010101
        else if (lbbm.length() == 6) {
            String dlCode = lbbm.substring(0, 2);
            String zlCode = lbbm.substring(0, 4);
            cond.setDl_id(dlCode);
            cond.setZl_id(zlCode);
            cond.setXl_id(lbbm);
        }
        //正常情形下：如果新增一个小类，dpLst是恒定为空的，直接新增就可以，无需判断
        dpLst = this.getDataPermissionLstByCond(cond);
        if (!CollectionUtils.isEmpty(dpLst)) {            //如果目前权限表中已经存在含有编码字段的记录,先将其删除
            this.deleteDataPermission(cond);
        }
        this.insertDataPermission(cond);        //直接新增大类
    }

    private void deleteDataPermission(String lbbm, String userId, String permissionType) {
        SysDataPermission cond = new SysDataPermission();
        cond.setUser_id(userId);
        cond.setPermission_type(permissionType);
        List<SysDataPermission> dpLst = new ArrayList<SysDataPermission>();
        //如果删除一大类
        if (lbbm.length() == 2) {
            cond.setDl_id(lbbm);
        }
        //如果新增一中类
        else if (lbbm.length() == 4) {    //新增一中类
            String dlCode = lbbm.substring(0, 2);
            cond.setDl_id(dlCode);
            cond.setZl_id(lbbm);
        }
        //如果新增一小类
        else if (lbbm.length() == 6) {
            String dlCode = lbbm.substring(0, 2);
            String zlCode = lbbm.substring(0, 4);
            cond.setDl_id(dlCode);
            cond.setZl_id(zlCode);
            cond.setXl_id(lbbm);
        }
        dpLst = this.getDataPermissionLstByCond(cond);
        if (!CollectionUtils.isEmpty(dpLst)) {
            this.deleteDataPermission(cond);
        }
    }

    private void deleteDataPermission(SysDataPermission permission) {
        sysDataPermissionDao.deleteDataPermission(permission);
    }

    public void insertDataPermission(SysDataPermission permission) {
        sysDataPermissionDao.insertDataPermission(permission);
    }

    private String[] toLbPermissionArray(String[] permissionArray) {
        List<String> list = new ArrayList<String>();
        for (int i = (permissionArray.length) - 1; i >= 0; i--) {
            list.add(permissionArray[i]);
        }
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if ((list.get(i).split("\\|\\|")[0]).startsWith((list.get(j).split("\\|\\|")[0]))) {
                    list.remove(j);
                }
            }
        }
        String[] newarray = new String[list.size()];
        int count = list.size();
        for (int i = 0; i < list.size(); i++) {
            count--;
            newarray[i] = list.get(count);
        }
        return newarray;
    }

    @Override
    public int deleteUserWlPermission(Map<String, Object> map) {
        return sysDataPermissionDao.deleteUserWlPermission(map);
    }

}
